package com.tekj.cxqcb.view.Module.activity.StoreModule;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.entity.LocalMedia;
import com.squareup.picasso.Picasso;
import com.tekj.cxqcb.MainBActivity;
import com.tekj.cxqcb.R;
import com.tekj.cxqcb.operation.BusinessDao;
import com.tekj.cxqcb.operation.resultBean.ApiShopServiceSelectAllBean;
import com.tekj.cxqcb.operation.resultBean.ApiShopServiceTypeBean;
import com.tekj.cxqcb.operation.resultBean.PubStatusBean;
import com.tekj.cxqcb.operation.resultBean.ShopGoodsListBean;
import com.tekj.cxqcb.operation.resultBean.UploadFiles;
import com.tekj.cxqcb.view.Module.activity.GoodsLevelActivity;
import com.tekj.cxqcb.view.Module.bean.ApiShopServiceInsertBean;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import common.base.activity.BaseActivity;
import common.base.operationHelper.Bean.BindingViewBean;
import common.tool.Tools;
import es.dmoral.toasty.Toasty;

/**
 * 新增商品
 */
public class AddStoreActivity extends BaseActivity {
    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_l)
    TextView tvL;
    @BindView(R.id.ll_back)
    LinearLayout llBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_r)
    ImageView ivR;
    @BindView(R.id.tv_r)
    TextView tvR;
    @BindView(R.id.ll_right)
    LinearLayout llRight;
    @BindView(R.id.ll_scj)
    LinearLayout llScj;
    @BindView(R.id.rl_title_bg)
    RelativeLayout rlTitleBg;
    @BindView(R.id.et_goods_name)
    EditText etGoodsName;
    @BindView(R.id.tv_type)
    TextView tvType;
    @BindView(R.id.img_type1)
    ImageView imgType1;
    @BindView(R.id.ll_type1)
    LinearLayout llType1;
    @BindView(R.id.img_type2)
    ImageView imgType2;
    @BindView(R.id.ll_type2)
    LinearLayout llType2;
    @BindView(R.id.et_Market_value)
    EditText etMarketValue;
    @BindView(R.id.et_price)
    EditText etPrice;
    @BindView(R.id.tv_Vehicle_type)
    TextView tvVehicleType;
    @BindView(R.id.et_content)
    EditText etContent;
    @BindView(R.id.tv_Word_number)
    TextView tvWordNumber;
    @BindView(R.id.img_photo)
    ImageView imgPhoto;
    @BindView(R.id.btn_Preservation)
    TextView btnPreservation;
    @BindView(R.id.et_Stock)
    EditText etStock;
    @BindView(R.id.img_photo1)
    ImageView imgPhoto1;
    @BindView(R.id.img_photo2)
    ImageView imgPhoto2;

    private BusinessDao businessDao;
    //    private ApiShopServiceSelectAllBean.DataBean.ListBean data;
    private String Shopid, name1, name2;
    private ShopGoodsListBean.DataBean.TypeLevelList.ListBean data;

    @Override
    public void init() {
        businessDao = new BusinessDao(activity, this);
        Shopid = MainBActivity.User.getShopId();
//        name1 = getIntent().getStringExtra("name1");
//        name2 = getIntent().getStringExtra("name2");
        data = (ShopGoodsListBean.DataBean.TypeLevelList.ListBean) getIntent().getSerializableExtra("data");
        etContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                tvWordNumber.setText(s.length() + "/200");
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        if (data != null) {
            tvTitle.setText("商品编辑");
            setViewData();
        } else {
            tvTitle.setText("商品新增");
            setOnline(true);
        }


    }

    @Override
    public int setLayout() {
        return R.layout.activity_add_store;
    }

    @Override
    public void initLoadStart() {
        showDialog();
    }

    @Override
    public void initLoadEnd() {
        dialog.dismiss();
    }

    @Override
    public void initBindingView(Object mBean) {
        BindingViewBean bindingViewBean = (BindingViewBean) mBean;
        switch (bindingViewBean.getmEnumStatus()) {
            case 多文件上传:
                UploadFiles uploadFiles = (UploadFiles) bindingViewBean.getBean();
                if (uploadFiles.getTypeName().equals("商品配图")) {
                    imgString = uploadFiles.getData().getUploadFilePath();
                } else if (uploadFiles.getTypeName().equals("商品大图")) {
                    imgString2 = uploadFiles.getData().getUploadFilePath();
                } else if (uploadFiles.getTypeName().equals("商品头像")) {
                    imgString1 = uploadFiles.getData().getUploadFilePath();
                }
                break;
            case 新增店铺商品:
                PubStatusBean pubStatusBean = (PubStatusBean) bindingViewBean.getBean();
                if (pubStatusBean.getCode().equals("0000000")) {
                    finish();
                }
                Toasty.normal(activity, pubStatusBean.getMsg()).show();
                break;
            case 修改店铺商品信息:
                PubStatusBean statusBean = (PubStatusBean) bindingViewBean.getBean();
                if (statusBean.getCode().equals("0000000")) {
                    finish();
                }
                Toasty.normal(activity, statusBean.getMsg()).show();
                break;

        }
    }


    @OnClick({R.id.ll_back, R.id.tv_type, /*R.id.ll_type1, R.id.ll_type2,*/
            R.id.tv_Vehicle_type, R.id.img_photo2, R.id.img_photo1, R.id.img_photo, R.id.btn_Preservation})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_back:
                finish();
                break;
            case R.id.tv_type:
                //TODO 分类选择
                startActivityForResult(
                        new Intent(activity, ClassificationActivity.class)
                                .putExtra("type", 1),
                        1001);
                break;
            case R.id.ll_type1:
                //TODO 选择线上支付
                setOnline(true);
                break;
            case R.id.ll_type2:
                //TODO 选择线下支付
                setOnline(false);
                break;
            case R.id.tv_Vehicle_type:
                //TODO 选择适用车型
                break;
            case R.id.img_photo:
                Tools.InitPictureSelector(activity, false, 1002, null, 1);
                break;
            case R.id.img_photo1:
                Tools.InitPictureSelector(activity, false, 1003, null, 1);
                break;
            case R.id.img_photo2:
                Tools.InitPictureSelector(activity, false, 1004, null, 1);
                break;
            case R.id.btn_Preservation:
                Tools.showNormalDialogOne(activity, data != null ? "保存" : "新增商品", "是否保存商品", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Updata();
                    }
                }, null);
                break;
        }
    }

    boolean isOnline;

    void setOnline(boolean online) {
        isOnline = online;
        imgType1.setImageResource(online ? R.mipmap.img_gx : R.mipmap.img_wgx2);
        imgType2.setImageResource(!online ? R.mipmap.img_gx : R.mipmap.img_wgx2);

        llScj.setVisibility(online ? View.VISIBLE : View.GONE);

    }

    void Updata() {

        ApiShopServiceInsertBean bean = new ApiShopServiceInsertBean();

        bean.setShopId(Shopid);
        if (etGoodsName.getText().toString().equals("")) {
            Toasty.normal(activity, "请填写商品名称").show();
            return;
        }
        bean.setServiceName(etGoodsName.getText().toString());

        if (tvType.getText().toString().equals("")) {
            Toasty.normal(activity, "请选择分组").show();
            return;
        }
        bean.setLevelOne(id);
        bean.setLevelTwo(id2);

        bean.setCarType("通用");
        if (etMarketValue.getText().toString().equals("") && isOnline) {
            Toasty.normal(activity, "请输入市场价").show();
            return;
        }
        bean.setMarketPrice(etMarketValue.getText().toString());

        if (etPrice.getText().toString().equals("")) {
            Toasty.normal(activity, "请输入售价").show();
            return;
        }
        bean.setPrice(etPrice.getText().toString());

        if (etStock.getText().toString().equals("")) {
            Toasty.normal(activity, "请输入库存").show();
            return;
        }
        bean.setInventory(etStock.getText().toString());

        if (etContent.getText().toString().equals("")) {
            Toasty.normal(activity, "请输入服务内容说明").show();
            return;
        }
        bean.setServiceExplain(etContent.getText().toString());

        if (/*imgString == null || imgString.equals("")||*/
                imgString1 == null || imgString1.equals("") ||
                        imgString2 == null || imgString2.equals("")) {
            Toasty.normal(activity, "请选择添加图片").show();
            return;
        }
        bean.setServiceImg(imgString);
        bean.setServiceImg2(imgString1);
        bean.setServiceImg3(imgString2);

        bean.setSettlementType(isOnline ? 1 : 2);

        bean.setId(data != null ? data.getId() : "");
        bean.setServiceState(data != null ? data.getServiceState() : 1);

        if (data == null)
            businessDao.ApiShopServiceInsert(bean);
        else
            businessDao.ApiShopServiceUpdate(bean);
    }

    String id, id2, imgString, imgString1, imgString2;
    private LocalMedia mediaphoto;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        List<File> list;
        if (resultCode == RESULT_OK)
            switch (requestCode) {
                case 1001:
                    ApiShopServiceTypeBean.DataBean level1, level2;
                    level1 = (ApiShopServiceTypeBean.DataBean) data.getSerializableExtra("level1");
                    level2 = (ApiShopServiceTypeBean.DataBean) data.getSerializableExtra("level2");

                    tvType.setText(level1.getTypeName() + " " + level2.getTypeName());
                    id = level1.getId();
                    id2 = level2.getId();
                    break;
                case 1002:
                    mediaphoto = PictureSelector.obtainMultipleResult(data).get(0);
                    Tools.picasso(mediaphoto.getCompressPath(), imgPhoto);
                    list = new ArrayList<>();
                    list.add(new File(mediaphoto.getCompressPath()));
                    businessDao.uploadFiles(list, "商品配图");
                    break;
                case 1003:
                    mediaphoto = PictureSelector.obtainMultipleResult(data).get(0);
                    Tools.picasso(mediaphoto.getCompressPath(), imgPhoto1);
                    list = new ArrayList<>();
                    list.add(new File(mediaphoto.getCompressPath()));
                    businessDao.uploadFiles(list, "商品头像");
                    break;
                case 1004:
                    mediaphoto = PictureSelector.obtainMultipleResult(data).get(0);
                    Tools.picasso(mediaphoto.getCompressPath(), imgPhoto2);
                    list = new ArrayList<>();
                    list.add(new File(mediaphoto.getCompressPath()));
                    businessDao.uploadFiles(list, "商品大图");
                    break;
            }
    }

    /**
     * 修改时
     */
    void setViewData() {
        etGoodsName.setText(data.getServiceName());
        etPrice.setText(data.getPrice() + "");
        etStock.setText(data.getInventory());
        etMarketValue.setText(data.getMarketPrice() + "");
        etContent.setText(data.getServiceExplain());
        imgString = data.getServiceImg();
        imgString1 = data.getServiceImg2();
        imgString2 = data.getServiceImg3();
        Tools.picassoNetwork(imgString, imgPhoto);
        Tools.picassoNetwork(imgString1, imgPhoto1);
        Tools.picassoNetwork(imgString2, imgPhoto2);
        setOnline(data.getSettlementType() == 1);
        id = data.getLevelOne();
        id2 = data.getLevelTwo();
        tvType.setText(name1 + " " + name2);
    }

}
